<?php
namespace App\Http\Controllers\ApiXcx;

use App\Http\Controllers\Controller;

class BaseController extends Controller
{
    //小程序基础控制器

    /**
     * 接口返回
     */
    public function toJson($code = 0, $msg = '', $data = [], $total = 0)
    {
        if ($code == -1) $code = -101;
        if ($code == -2) $code = -102;
        if ($code == 0) $code = 200;
        if (!$msg) $msg = 'success';
        return [
            'code' => $code,
            'msg' => $msg,
            'data' => $data,
            'total' => $total,
        ];
    }

    /**
     * 自定义验证
     * @param array $pas 请求参数
     * @param array $fields 字段信息
     */
    public function checkField($request, $fields)
    {
        $pas = $request->all();
        $fkeys = [];
        $fieldArys = [];
        foreach ($fields as $k => $v) {
            $fieldArys[$v['en']] = isset($pas[$k]) ? $pas[$k] : '';
            if ((!isset($pas[$k]) || !$pas[$k])
                && (isset($v['ismust']) && $v['ismust'])) {
                //判断字段的值有没有，并且看看字段是否必须的
                $fkeys[] = $fields[$k]['cn'] . '必须的';
            }
        }
        if (count($fkeys) > 0) {
            return array('code' => -2, 'msg' => $fkeys[0], 'data' => $fieldArys);
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $fieldArys);
    }

    //获取用户手机端IP
    public function getUserIp()
    {
        $clientIP = '';
        if (isset($_SERVER['HTTP_CLIENT_IP'])) {
            // 如果使用了代理，通过这种方式获取真实的IP地址
            $clientIP = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            // 对于使用了CDN或者反向代理的请求，可能需要从HTTP头中获取真实的IP地址
            $clientIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
            // 没有代理的情况下直接获取IP地址
            $clientIP = $_SERVER['REMOTE_ADDR'];
        }
        return $clientIP;
    }

//    /**
//     * 构建CURL请求，单线程
//     * @param $url 请求地址
//     * @param $string 参数拼接的字符串
//     * @param $method 请求方式：put、post、get、delete
//     * @return $json
//     */
//    public function setCurl($url='', $string='', $method='get')
//    {
//        if (!$url) {
//            return array('code'=>-2 ,'msg'=>'没有接口地址');
//        }
//        if (!$method) {
//            return array('code'=>-2 ,'msg'=>'没有请求方式');
//        }
//        $curl = curl_init(); //初始化
//        curl_setopt($curl,CURLOPT_URL,$url);
//        curl_setopt($curl, CURLOPT_HEADER, 0);
//        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
//        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
//        if ($method=='post') {
//            curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
//            curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
//            curl_setopt($curl, CURLOPT_ENCODING, '');
//            curl_setopt($curl, CURLOPT_POST, 1);
//            curl_setopt($curl, CURLOPT_POSTFIELDS, $string);
//            curl_setopt($curl, CURLOPT_HTTPHEADER, array(
//                'Content-Type: application/json', 'Content-Length: ' . strlen($string)
//            ));
////        } else if ($method=='put') {
////            curl_setopt($curl, CURLOPT_PUT, 1);
////        } else if ($method=='delete') {
////        } else {
//        }
//        curl_setopt($curl, CURLOPT_TIMEOUT, 5);
//        $output = curl_exec($curl);
//        curl_close($curl);
////        ddd($output);
//        if (!$output) {
//            return array('code'=>-1, 'msg'=>'请求失败');
//        }
//        $items = json_decode($output,true);
//        return array('code'=>0,'msg'=>'success','data'=>$items);
//    }
}